package com.heu.blood.dict.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.dict.entity.DictDataEntity;
import com.heu.blood.dict.entity.DictTypeEntity;
import com.heu.blood.dict.service.DictDataService;
import com.heu.blood.dict.service.DictTypeService;
import com.heu.blood.dict.vo.DictTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author lrz
 * @email lrz@heu.com
 * @date 2023-07-30 21:48:23
 */
@RestController
@RequestMapping("dict/dicttype")
public class DictTypeController {
    @Autowired
    private DictTypeService dictTypeService;

    @Autowired
    private DictDataService dictDataService;

    /**
     * 分页条件查询字典管理数据信息
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "分页条件查询字典管理数据信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dictTypeService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 条件查询字典管理数据信息
     * @param dictTypeEntity
     * @return
     */
    @GetMapping("/query")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "条件查询字典管理数据信息")
    public R query(@RequestBody DictTypeEntity dictTypeEntity){
        List<DictTypeEntity> dictTypeEntityList = dictTypeService.query(dictTypeEntity);
        if (dictTypeEntityList.size()==0 || dictTypeEntityList.isEmpty()){
            return R.error("未查询到字典管理数据！");
        }
        return R.ok().put("list", dictTypeEntityList);
    }

//    查询字典管理数据信息
    @GetMapping("/queryDictTypeList")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "查询字典管理数据信息")
    public R queryDictTypeList(){
        List<DictTypeVo> list = dictTypeService.queryDictTypeList();

        return R.ok().put("list", list);
    }

    /**
     * 根据字典类型查询具体字典数据项
     * @param dictType
     * @return
     */
    @GetMapping("/selectDictData")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "根据字典类型查询具体字典数据项")
    public R selectDictData(String dictType){
        List<DictDataEntity> dictDataEntityList = dictDataService.selectDictData(dictType);
        if (dictDataEntityList.size()==0 || dictDataEntityList.isEmpty()){
            return R.error("未查询到相关字典数据项信息");
        }
        return R.ok().put("list", dictDataEntityList);
    }

    /**
     * 根据id查询字典管理数据信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "根据id查询字典管理数据信息")
    public R info(@PathVariable("id") Long id){
		DictTypeEntity dictType = dictTypeService.getById(id);
        return R.ok().put("dictType", dictType);
    }

    /**
     * 保存字典管理数据信息
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "保存字典管理数据信息")
    public R save(@RequestBody DictTypeEntity dictType){
		dictTypeService.save(dictType);
        return R.ok();
    }

    /**
     * 修改字典管理数据信息
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "修改字典管理数据信息")
    public R update(@RequestBody DictTypeEntity dictTypeEntity){
		String msg = dictTypeService.updateDictById(dictTypeEntity);
        return R.ok(msg);
    }

    /**
     * 删除字典管理数据信息
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "字典类型信息", operate = "删除字典管理数据信息")
    public R delete(@RequestBody Long[] ids){
		dictTypeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
